import sys
import mainUi
from PyQt5.QtWidgets import QApplication, QDialog, QFileDialog
from data_format import  load_file_data,show_data
from logistic_regression import logistic_regression_train,LR_result,show_change_logistic_regression
from knn import knn_train,KNN_result,show_change_knn
from naivebayes import  naive_bayyes_train,NB_result
from SVM import show_change_SVM,SVM_train,SVM_result
from ann import nerual_network_train,show_change_ann,ANN_result
from random_forest import  random_forest_train,show_change_random_forest,RF_result
import warnings
warnings.filterwarnings('ignore')  # "error", "ignore", "always", "default", "module" or "once"

class MainDialog(QDialog):
    def __init__(self, parent=None):
        super(QDialog, self).__init__(parent)
        self.ui = mainUi.Ui_Dialog()
        self.ui.setupUi(self)
        self.filename = ""
        self.model = "逻辑回归预测模型(LR)"
        self.test_model = "逻辑回归预测模型(LR)"
        self.x_train = ""
        self.y_train = ""
        self.x_cross = ""
        self.y_cross = ""
        self.x_test = ""
        self.y_test = ""
        self.c_list = ""
        self.accurancy_list = ""

    def dataBtnCliked(self):
        show_data(self.x_train,self.y_train,'Train Data')
        show_data(self.x_cross, self.y_cross, 'Cross Data')
        show_data(self.x_test, self.y_test, 'Test Data')

    def openfileBtn01Clicked(self):
        self.filename, filetype = QFileDialog.getOpenFileName(self, '选择文件', "/",
                                                              "All Files (*);;Excel files (*.arff, *.xlsx , *.xls , *.csv)")
        self.x_train, self.y_train, self.x_cross, self.y_cross, self.x_test, self.y_test = load_file_data(self.filename)
        self.ui.label02.setText(self.filename)

    def trainBtnClicked(self):
        self.model = self.ui.trainComBox.currentText()
        if self.model == '逻辑回归预测模型(LR)':
            self.c_list,self.accurancy_list = logistic_regression_train(self.x_train, self.y_train, self.x_cross, self.y_cross, self.x_test, self.y_test)
        elif self.model == 'K邻近模型(KNN)':
            self.c_list,self.accurancy_list = knn_train(self.x_train, self.y_train, self.x_cross, self.y_cross, self.x_test, self.y_test)
        elif self.model == '朴素贝叶斯模型(NB)':
            naive_bayyes_train(self.x_train, self.y_train, self.x_cross, self.y_cross, self.x_test, self.y_test)
        elif self.model == '支持向量机模型(SVM)':
            SVM_train(self.x_train, self.y_train, self.x_cross, self.y_cross, self.x_test, self.y_test)
        elif self.model == '人工神经网路(ANN)':
            self.c_list,self.accurancy_list = nerual_network_train(self.x_train, self.y_train, self.x_cross, self.y_cross, self.x_test, self.y_test)
        elif self.model == '随机森林模型(RF)':
            self.c_list,self.accurancy_list = random_forest_train(self.x_train, self.y_train, self.x_cross, self.y_cross, self.x_test, self.y_test)

        print('model:{}'.format(self.model))

    def trainResultBtnCliked(self):
        if self.model == '逻辑回归预测模型(LR)':
            # 绘制c 和 accurancy 的变化曲线
            show_change_logistic_regression(self.c_list,self.accurancy_list)
        elif self.model == 'K邻近模型(KNN)':
            show_change_knn(self.c_list,self.accurancy_list)
        elif self.model == '朴素贝叶斯模型(NB)':
            print("朴素贝叶斯模型(NB)")
        elif self.model == '支持向量机模型(SVM)':
            show_change_SVM()
        elif self.model == '人工神经网路(ANN)':
            show_change_ann(self.c_list,self.accurancy_list)
        elif self.model == '随机森林模型(RF)':
            show_change_random_forest(self.c_list,self.accurancy_list)

    def openfileBtn02Cliked(self):
        openfile_name, filetype = QFileDialog.getOpenFileName(self, '选择文件', "/",
                                                              "All Files (*);;Excel files (*.arff, *.xlsx , *.xls , *.csv)")
        self.x_train, self.y_train, self.x_cross, self.y_cross, self.x_test, self.y_test = load_file_data(self.filename)
        self.ui.label06.setText(openfile_name)

    def testBtnCliked(self):
        self.test_model = self.ui.testComBox.currentText()
        print('test_model:{}'.format(self.test_model))

    def testResultBtnCliked(self):
        if self.test_model == '逻辑回归预测模型(LR)':
            LR_result(self.x_test, self.y_test)
        elif self.test_model == 'K邻近模型(KNN)':
            KNN_result(self.x_train, self.y_train, self.x_test, self.y_test)
        elif self.test_model == '朴素贝叶斯模型(NB)':
            NB_result(self.x_test, self.y_test)
        elif self.test_model == '支持向量机模型(SVM)':
            SVM_result()
        elif self.test_model == '人工神经网路(ANN)':
            ANN_result(self.x_test, self.y_test)
        elif self.test_model == '随机森林模型(RF)':
            RF_result(self.x_test, self.y_test)


if __name__ == '__main__':
    #实例化主窗口
    app = QApplication(sys.argv)
    mainDlg = MainDialog()
    mainDlg.show()

    sys.exit(app.exec_())